<?php
/**
 * @package		JYCMS
 * @subpackage	插件模型
 * @version		$Id: model_plugins.php 29 2013-05-07 06:45:56Z hopingsoft@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2009 Hoping Software Studio. All rights reserved.
 */

// 保证此文件不被直接执行
defined ( 'IN_JYCMS' ) or die ( 'Direct Access to this location is not allowed.' );
class Plugins extends cmsDBModel {
	var $pluginid = null;
	var $available = null;
	var $name = null;
	var $description = null;
	var $directory = null;
	var $copyright = null;
	var $version = null;
	var $group = null;
	var $_events = null;
	var $_lists = null;
	var $_bots = null;
	var $_loading = null;
	var $_groups = null;
	var $_pluginvars = null;
	function __construct() {
		$this->plugins ();
	}
	function plugins() {
		$this->cmsDBTable ( '#__common_plugins', 'pluginid' );
		$this->_events = array ();
	}
	function load($oid = null, $k = '') {
		global $control;
		$row = parent::load ( $oid, $k );
		if (! $row) {
			return false;
		}
		$pid = $row ['pluginid'];
		$sql = "SELECT * FROM #__common_pluginvars WHERE pluginid='$pid' ORDER BY displayorder";
		DB::query ( $sql );
		$rows = DB::fetch_all ();
		$row ['pluginvars'] = $this->_pluginvars = $rows;
		return $row;
	}
	/**
	 * 加载一组插件文件
	 *
	 * @param
	 *        	组名。
	 */
	function loadPlugins($group) {
		global $control, $_G;
		if ($this->_groups != $group) {
			$group = trim ( $group );
			$this->_groups = $group;
			$file = DATA_PATH . '/cache/plugins.php';
			if (! file_exists ( $file )) {
				loadcache ( 'plugins', true );
			} else {
				include $file;
			}
			$bots = @$_G ['cache'] ['plugins'] [$group];
			if ($bots)
				foreach ( $bots as $key => $var ) {
					if ($var ['available'] == 1) {
						if ($this->loadPlugin ( $var ['directory'], $var ['vars'] )) {
							return true;
						}
					}
				}
		}
		return false;
	}

	/*
	 * 加载插件文件
	 */
	function loadPlugin($folder, $params = '') {
		$path = ROOT_PATH . '/source/plugin/' . $folder . '/' . $folder . '.php';
		if (file_exists ( $path )) {
			require_once ($path);
			$classname = 'plugin' . $folder;
			$this->_loading = count ( $this->_bots );
			$bot = new $classname ( $params );
			$arr = get_class_methods ( $bot );
			foreach ( ( array ) $arr as $v ) {
				$v = strtolower ( $v );
				$vv = substr ( $v, 0, 2 );
				if ($vv == 'on') {
					$this->registerFunction ( substr ( $v, 2 ) );
				}
			}
			$this->_bots [] = $bot;
			return true;
		} else {
			return false;
		}
	}

	/*
	 * 注册插件中的动作
	 */
	function registerFunction($event) {
		$this->_events [$event] [] = $this->_loading;
	}

	/*
	 * 触发插件动作
	 */
	function trigger($event, $args = null) {
		$event = strtolower ( $event );
		if (isset ( $this->_events [$event] )) {
			foreach ( $this->_events [$event] as $key ) {
				$func = 'on' . $event;
				if (method_exists ( $this->_bots [$key], $func )) {
					$args = $this->_bots [$key]->$func ( $args );
				}
			}
		}
		return $args;
	}

	/*
	 * 调用插件中设置的触发事件
	 */
	function call($event) {
		$doUnpublished = false;
		$args = & func_get_args ();
		array_shift ( $args );
		if (isset ( $this->_events [$event] )) {
			foreach ( $this->_events [$event] as $func ) {
				if (function_exists ( $func [0] )) {
					if ($this->_bots [$func [1]]->published) {
						return call_user_func_array ( $func [0], $args );
					}
				}
			}
		}
		return null;
	}

	/**
	 * 检测数据是否合法，由各表例程自定义
	 *
	 * @return 判断结果
	 */
	function check() {
		if (! $this->directory) {
			$this->_error = '对不起，您没有指定要安装的插件！';
			return false;
		}
		if ($this->pluginid)
			$where = " AND pluginid!=" . $this->pluginid;
		$sql = "SELECT count(*) FROM #__common_plugins WHERE `directory`='" . $this->directory . "'";
		$result = $this->_db->result_first ( $sql );
		if ($result && ! $this->pluginid) {
			$this->_error = '对不起，您已经安装过相同的插件了！';
			return false;
		}
		return true;
	}
	function get_plugins() {
		global $control;
		require_once libfile ( 'class/xml' );
		$arr = array ();
		$dir = ROOT_PATH . './source/plugin';
		$d = opendir ( $dir );
		while ( $f = readdir ( $d ) ) {
			if ($f != '.' && $f != '..' && $f != '.svn' && is_dir ( $dir . '/' . $f )) {
				$s = file_get_contents ( $dir . '/' . $f . '/' . $f . '.xml' );
				$arr1 = xml2array ( $s );
				$arr1 ['dir'] = $f;
				$arr [] = $arr1;
			}
		}
		return $arr;
	}
	function get_plugin($pluginname) {
		$pf = ROOT_PATH . "./source/plugin/$pluginname/$pluginname.xml";
		if (! file_exists ( $pf )) {
			return false;
		}
		$f = file_get_contents ( ROOT_PATH . "./source/plugin/$pluginname/$pluginname.xml" );
		require_once libfile ( 'class/xml' );
		$f = xml2array ( $f );
		$f ['directory'] = $pluginname;
		return $f;
	}
	function get_plugin_by_name($pluginname) {
		$dir = ROOT_PATH . './source/plugin';
		$s = file_get_contents ( $dir . '/' . $pluginname . '/plugin.xml' );
		return xml_unserialize ( $s, TRUE );
	}
	function orderby_tabindex($arr1) {
		$arr2 = array ();
		$t = array ();
		foreach ( ( array ) $arr1 as $k => $v ) {
			$t [$k] = $v ['tabindex'];
		}
		asort ( $t );
		$arr3 = array ();
		foreach ( ( array ) $t as $k => $v ) {
			$arr3 [$k] = $arr1 [$k];
		}
		return $arr3;
	}
}
